<template>
  <div>
    <base-list ref="storageSet"
               :condition="condition"
               :datas.sync="storageSet"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               label-width="80px">
        <el-row>
          <el-col :span="10" class="search-width4">
            <el-form-item label="所属仓库">
              <el-select v-model="condition['search_EQ_warehouse.id']"
                         placeholder="请选择"
                         clearable>
                <el-option v-for="item in warehouseList"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" class="search-width4">
            <el-form-item label="库房名称">
              <auto-search-room-name @select="v=>condition['search_LIKE_roomName']=v"></auto-search-room-name>
            </el-form-item>
          </el-col>
          <el-col :span="10" class="search-width4">
            <el-form-item label="库房代码">
              <auto-search-room-code @select="v=>condition['search_LIKE_roomCode']=v"></auto-search-room-code>
            </el-form-item>
          </el-col>
          <el-col :span="10" class="search-width4">
            <el-form-item label="所属单位">
              <auto-search-org v-model="form.orgId"
                               @select="v=>{form.orgId=v;form.deptId='';form.deptName='';}"
                               @selectName="v=>form.orgName=v"
                               :disabled="true"
                               :name="form.orgName"></auto-search-org>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary"
                       icon="el-icon-search"
                       @click="loadData"
                       class="ml-80">查 询</el-button>
            <!-- <el-button formVisible="true" icon="el-icon-plus" @click="handleDialogFormOpen(false)" v-if="power('WJOB_BASEMGR_VEHICLE:ADD')">
                            新增
                        </el-button> -->
          </el-col>
        </el-row>
      </el-form>
      <el-table slot="list"
                :data="storageSet"
                border
                stripe
                max-height="550">
        <el-table-column type="index"
                         fixed="left"
                         label="序号"
                         width="55">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column>
        <el-table-column prop="roomCode"
                         label="库房代码"
                         min-width="120"></el-table-column>
        <el-table-column prop="roomName"
                         label="库房名称"
                         min-width="120"></el-table-column>
        <el-table-column prop="warehouseName"
                         label="所属仓库"
                         min-width="120"></el-table-column>
        <el-table-column prop="orgName"
                         label="所属单位"
                         min-width="150"></el-table-column>
        <el-table-column prop="roomTypeDesc"
                         label="库房类型"
                         min-width="120"></el-table-column>
        <el-table-column fixed="right"
                         align="center"
                         label="操作"
                         width="140">
          <template slot-scope="scope">
            <el-button size="small"
                       type="text"
                       @click="handleDialogFormOpen(true,scope.row)">
              编辑
            </el-button>
            <el-button type="text"
                       class="danger"
                       size="small"
                       @click="handleRemove(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </base-list>
    <storage-set-add-form ref="storageSetAddForm"
                          @load="loadData"></storage-set-add-form>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 plain
                 @click="handleRouterPush({path: '/goods/storehouse',name: '仓库管理'})">返回</el-button>
    </div>
  </div>
</template>

<script>
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse' //所属仓库来源于仓库
import {
  deleteStorageSet,
  getStorageSetList
} from '@/api/storageSet/storageSet'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchRoomCode from '@/components/autocomplete/autoSearchRoomCode'
import autoSearchRoomName from '@/components/autocomplete/autoSearchRoomName'
import storageSetAddForm from './storageSetAddForm'

export default {
  name: 'storageSet',
  components: {
    storageSetAddForm,
    autoSearchOrg,
    autoSearchRoomCode,
    autoSearchRoomName
  },
  data() {
    return {
      form: this.clearForm(),
      condition: {}, // 查询条件
      storageSet: [], // table数组
      pageSearch: getStorageSetList, // 分页查询api接口
      id: '',
      warehouseList: null
    }
  },
  mounted() {
    getWarehouseList().then(res => {
      this.warehouseList = res.data.aaData
    }),
      this.$nextTick(() => {
        this.loadData()
      })
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name
      }
    },
    loadData() {
      this.$refs.storageSet.loadData()
    },
    handleDialogFormOpen(bool, item) {
      if (bool) {
        this.$refs.storageSetAddForm.form = JSON.parse(JSON.stringify(item))
        this.$refs.storageSetAddForm.form.maxSpan =
          '' + this.$refs.storageSetAddForm.form.maxSpan
        this.$refs.storageSetAddForm.form.maxAula =
          '' + this.$refs.storageSetAddForm.form.maxAula
        this.$refs.storageSetAddForm.isEdit = true
        this.$refs.storageSetAddForm.dialogForm = true
      } else {
        this.$refs.storageSetAddForm.dialogForm = true
      }
    },
    handleRemove(item) {
      this.$confirm(`是否删除该数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteStorageSet(item.id).then(res => {
            console.log(item.id)
            this.$message({
              type: 'success',
              message: `删除成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style scoped>
</style>
